<template>
  <div class="product-detail">
    <el-button type="primary" class="back-button" @click="goBack" size="mini"
      ><i class="el-icon-arrow-left">返回</i></el-button
    >
    <el-card class="product-card">
      <div slot="header" class="clearfix" style="text-align: center">
        <span class="product-title">{{ product.product_name }}</span>
      </div>
      <div style="display: flex; justify-content: center">
        <div>
          <el-image :src="product.img" alt="商品图片" class="product-image" />
        </div>
        <div class="product-info">
          <p><strong>商品ID:</strong> {{ product.product_id }}</p>
          <p v-if="loginPerson.userType === 'supplier'">
            <strong>进价:</strong> {{ product.purchase_price }} 元
          </p>
          <p><strong>售价:</strong> {{ product.price }} 元</p>
          <p><strong>库存:</strong> {{ product.inventory }} 件</p>
          <p><strong>销量:</strong> {{ product.sales_volume }} 件</p>
          <p><strong>生产日期:</strong> {{ product.production_date }}</p>
          <p><strong>供应商:</strong> {{ product.supplier_id }}</p>
        </div>
      </div>
      <el-button
        type="success"
        class="add-to-cart-button"
        @click="addToCart"
        :disabled="!loginPerson"
      >
        <i class="el-icon-shopping-cart-2"></i>
        加入购物车
      </el-button>
    </el-card>
  </div>
</template>

<script>
// import { mapState } from "vuex";

export default {
  name: "ProductDetail",
  data() {
    return {
      product: {}, // 存储商品信息
    };
  },
  computed: {
    // ...mapState({
    //   goodsList: (state) => state.goods.goodsList, // 从 Vuex 中获取商品列表
    // }),
    goodsList() {
      return (
        JSON.parse(localStorage.getItem("vuex-goods")) ||
        this.$store.state.goods.goodsList
      );
    },
    loginPerson() {
      return (
        this.$store.state?.loginPerson ||
        JSON.parse(localStorage.getItem("loginPerson"))
      );
    },
  },
  created() {
    const productId = this.$route.params.id; // 从路由参数获取商品ID
    this.product = this.goodsList.find((item) => item.product_id === productId); // 查找对应商品
  },
  methods: {
    goBack() {
      this.$router.push("/store");
    },
    addToCart() {
      if (!this.loginPerson) {
        this.$message.error("请先登录");
        return;
      }

      const productToAdd = {
        img: this.product.img,
        product_id: this.product.product_id,
        product_name: this.product.product_name,
        price: this.product.price,
        supplier_id: this.product.supplier_id,
        supplier_name: this.product.supplier_name,
      };

      this.$store.dispatch("shoppingCart/addToCart", {
        userId: this.loginPerson.id,
        product: productToAdd,
      });

      this.$message.success("已加入购物车");
      this.$router.push("/store");
    },
  },
};
</script>

<style scoped>
.product-detail {
  padding: 40px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  min-height: 100vh;
}

.back-button {
  margin-bottom: 30px;
  border-radius: 8px;
  padding: 12px 25px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.product-card {
  border-radius: 16px;
  overflow: hidden;
  background: #ffffff;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}

.product-title {
  font-size: 32px;
  font-weight: 600;
  color: #2d3436;
  letter-spacing: 0.5px;
}

.product-image {
  width: 100%;
  /* height: 400px; */
  margin: 20px auto;
  border-radius: 12px;
  object-fit: cover;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.product-info {
  padding: 30px;
  background: #f8f9fa;
  border-radius: 12px;
  margin: 0;
}

.product-info p {
  margin: 15px 0;
  font-size: 16px;
  color: #495057;
  display: flex;
  align-items: center;
}

.product-info strong {
  min-width: 80px;
  color: #2d3436;
  margin-right: 15px;
}

.add-to-cart-button {
  margin-top: 30px;
  width: 100%;
  height: 50px;
  font-size: 18px;
  border-radius: 8px;
  letter-spacing: 1px;
  background: linear-gradient(135deg, #4cd964 0%, #34e89e 100%);
  border: none;
  transition: all 0.3s ease;
}

.add-to-cart-button:hover {
  transform: scale(1.02);
  box-shadow: 0 8px 20px rgba(76, 217, 100, 0.3);
}

@media (max-width: 768px) {
  .product-image {
    width: 100%;
    height: 300px;
  }

  .product-title {
    font-size: 24px;
  }
}
</style>
